package com.smarth.zhizulian.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.smarth.zhizulian.R;

/**
 * Created by 18865 on 2017/12/25.
 */

public class ShareMeDialog extends PopupWindow {
    private Activity mContext;
    private ImageView iv_wechat;
    private ImageView iv_qq;
    private ImageView iv_moment;
    private boolean mIsShowWechat;

    public ShareMeDialog(Activity context, boolean isShowWechat) {
        this.mContext = context;
        this.mIsShowWechat = isShowWechat;
        init();
    }

    public ShareMeDialog(Activity context) {
        this(context, true);
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.dialog_shaishouru, null);
        iv_wechat = contentView.findViewById(R.id.iv_wechat);
        iv_moment = contentView.findViewById(R.id.iv_moment);
        iv_qq = contentView.findViewById(R.id.iv_qq);
        iv_wechat.setVisibility(mIsShowWechat ? View.VISIBLE : View.GONE);
        iv_qq.setVisibility(mIsShowWechat ? View.VISIBLE : View.GONE);
        contentView.findViewById(R.id.v_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareMeDialog.this.dismiss();
            }
        });

        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(null);
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                backgroundAlpha(1f);
//            }
//        });
    }

    public void show() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
//        backgroundAlpha(0.6f);
    }

//    /**
//     * 设置添加屏幕的背景透明度
//     *
//     * @param bgAlpha
//     */
//    public void backgroundAlpha(float bgAlpha) {
//        Activity activity = (Activity) mContext;
//        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
//        lp.alpha = bgAlpha; //0.0-1.0
//        activity.getWindow().setAttributes(lp);
//    }

    public ShareMeDialog setOnClickWechatListener(final View.OnClickListener onClickListener) {
        iv_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.onClick(view);
                ShareMeDialog.this.dismiss();
            }
        });
        return this;
    }

    public ShareMeDialog setOnClickMomentListener(final View.OnClickListener onClickListener) {
        iv_moment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.onClick(view);
                ShareMeDialog.this.dismiss();
            }
        });
        return this;
    }

    public ShareMeDialog setOnClickQQListener(final View.OnClickListener onClickListener) {
        iv_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.onClick(view);
                ShareMeDialog.this.dismiss();
            }
        });
        return this;
    }

}
